<?php
include("../../tools/session.php");
include("../../tools/controls.php");
include("../../tools/pub.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
include("../../tools/mysql.php");
db_connect();

$page_status = $_POST["page_status"];
// $page_status = $_GET["page_status"];
$per_page = 10;

// 正常状态下的页面
if (!$page_status)
{
	include("tpl-inc-wp.php");
	
	$sql = "SELECT ceil(count(id)/" . $per_page . ") num FROM view_user_archive";
	$re = db_query_once($sql);
	
	$tpl->assign("page_num", $re["num"]);
	
	$tpl->display($oa_tpl_path . "/default/doc_template_class.html");
}
// 需要修改的员工信息提交过来时
// 页面参数 page_status 值为 1
elseif ($page_status == 1)
{
   $id = $_POST["id"];
   $sql = "SELECT * FROM doc_tpl_class WHERE tpl_class_id=" . $id;
   $re = db_query_once($sql);

   header("content-type: text/xml");
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   echo "<class>\n";
   
   echo "<id><![CDATA[" . $re["tpl_class_id"] . "]]></id>";
   echo "<name><![CDATA[" . $re["name"] . "]]></name>";
   echo "<zhname><![CDATA[" . $re["zh_name"] . "]]></zhname>\n";
   echo "<sequence><![CDATA[" . $re["sequence"] . "]]></sequence>\n";
   
   echo "</class>\n";
}
// 分页时
// 页面参数 page_status 值为 2
elseif ($page_status == 2)
{
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<class>\n";

	$sql = "SELECT * FROM doc_tpl_class ORDER BY sequence ASC";
	$re = db_query($sql);
    
	$str = "";
	foreach ($re as $v)
	{
	    $str .= "<item>\n";
	    $str .= "<id>" . $v["tpl_class_id"] . "</id>\n";
	    $str .= "<name>" . $v["name"] . "</name>\n";
	    $str .= "<zhname>" . $v["zh_name"] . "</zhname>";
	    $str .= "<sequence>" . $v["sequence"] . "</sequence>";
	    $str .= "</item>\n";
	}
	
	echo $str;
	
	echo "</class>\n";
}
// 提交修改数据时
// 页面参数 page_status 为 3
elseif($page_status == 3)
{
    $tpl_class_id = $_POST["m_id"];
    $name = $_POST["m_en_name"];
    $zh_name = $_POST["m_zh_name"];
    $sequence = $_POST["m_sequence"];

    $sql = "UPDATE doc_tpl_class SET name='" . $name . "', zh_name='" . $zh_name . "', sequence=" . $sequence . " WHERE tpl_class_id=" . $tpl_class_id;
    $log_str = array("修改公文模板类（ID: " . $tpl_class_id . "）内容成功！", "修改公文模板类（ID: " . $tpl_class_id . "）内容失败！");
    echo check_status_write_to_log(db_exec($sql), $log_str, FALSE);
}
// 提交增加数据时
// 页面参数 page_status 为 4
elseif($page_status == 4)
{
    $name = $_POST["a_en_name"];
    $zh_name = $_POST["a_zh_name"];
    $sequence = $_POST["a_sequence"];
    
    $sql = "INSERT INTO doc_tpl_class(name, zh_name, sequence) VALUES('" . $name . "', '" . $zh_name . "', " . $sequence . ")";
    $log_str = array("添加公文模板类“" . $zh_name . "”成功！", "添加公文模板类失败！");
    echo check_status_write_to_log(db_exec($sql), $log_str, TRUE);
}
// 删除公文模板类
elseif ($page_status == 5)
{
	$id = $_POST["id"];
	
	$sql = "DELETE FROM doc_tpl_class WHERE tpl_class_id=" . $id;
	// echo db_exec($sql) ? "success!" : "failure!";
	$log_str = array("删除公文模板类（ID: " . $id . "）成功！", "删除公文模板类（ID: " . $id . "）失败！");
	echo check_status_write_to_log(db_exec($sql), $log_str, FALSE);
}
?>

